package com.qkm.xinguan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkm.xinguan.constant.SystemConst;
import com.qkm.xinguan.domain.entity.Notice;
import com.qkm.xinguan.domain.infrastructure.annotation.ControllerEndpoint;
import com.qkm.xinguan.domain.transform.dto.NoticeDTO;
import com.qkm.xinguan.domain.transform.form.NoticeSearchForm;
import com.qkm.xinguan.domain.transform.form.UserSearchForm;
import com.qkm.xinguan.exception.BindingResultException;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.NoticeRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.NoticeVo;
import com.qkm.xinguan.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author zfj
 * @date 2021/11/22 -21:10
 */
@RestController
public class NoticeController implements INoticeController{

	private final NoticeRepository noticeRepository;

    @Autowired
	public NoticeController(NoticeRepository noticeRepository) {
		this.noticeRepository = noticeRepository;
	}

	@Override
	public Result getOneById(Long id) {
		Notice notice = noticeRepository.getById(id);
		if (null != notice) {
			return Result.ok().data(notice);
		} else {
			throw new BusinessException(ResultCode.NOTICE_ACCOUNT_NOT_EXIST);
		}
	}

	@Override
	public Result getNoticePage(Integer page, Integer size) {
		return noticeRepository.getNoticeListPage(page,size);
	}
	@Override
	public Result getNoticeListPage(Integer page, Integer size) {
		return noticeRepository.getNoticeListPage(page,size);
	}

	@Override
	public Result searchNoticeListPage(NoticeSearchForm noticeSearchForm, Integer page, Integer size) {
		Page<Notice> pageInfo = noticeRepository.searchNoticeListPage(noticeSearchForm, page, size);
		return Result.ok().data(pageInfo);
	}

	@Override
	@ControllerEndpoint(exceptionMessage = "添加公告失败", operation = "公告管理[添加]")
	public Result addNotice(NoticeDTO noticeDTO, BindingResult result) {
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		boolean save=noticeRepository.save(executeEntity(noticeDTO,null));
			if (save) {
				return Result.ok().message("公告添加成功");
			} else {
				throw new BusinessException(ResultCode.ADD_Notice);
			}

	}

	public static Notice executeEntity(NoticeDTO noticeDTO,Long id){
		Notice notice = new Notice();
		BeanUtils.copyProperties(noticeDTO,notice);
		notice.setId(id);
		notice.setForbidden(SystemConst.ACTIVE);
		return notice;
	}

	@Override
	public Result editNotice(NoticeDTO noticeDTO, Long id, BindingResult result) {
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		return noticeRepository.update(id,noticeDTO);
	}

	@Override
	@ControllerEndpoint(exceptionMessage = "删除公告失败", operation = "公告管理[删除]")
	public Result deleteNotice(Long id) {
    	Notice notice=noticeRepository.getById(id);
    	if(Objects.isNull(notice)){
    		throw new BusinessException(ResultCode.NOTICE_ACCOUNT_NOT_EXIST);
	    }
    	boolean b=noticeRepository.removeById(id);
    	if(b){
		    return Result.ok().message("删除公告成功");
	    }
		throw new BusinessException("删除公告失败");
	}
}
